//logs.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var timeOut = 60;
Page({
  data: {
    tipsCode: "获取验证码",
    timeNum: 60,
    clikType: false,
    currentTab: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo')

  },
  onLoad: function () {
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {

            }
          })
        }
      }
    })
    var openId = wx.getStorageSync("OPENID");

    var SESSION_KEY = wx.getStorageSync('SESSION_KEY');


  },
  selectlogo: function (e) {
    var that = this;
    if (that.data.currentTab === e.currentTarget.dataset.index) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.index
      })
    }
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);

  },
  // 点击获取手机验证码
  clickGainCode(e) {
    if (!this.data.clikType) {
      this.gainCode();
    }
  },
  //输入手机号获取验证码
  gainCode() {
    var that = this;
    if (this.data.mobile == '' || this.data.mobile == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
    } else {
      that.setData({
        clikType: true
      })
      if (that.data.timeNum < timeOut) {} else {
        wx.request({
          url: applet_park_api_url + '/daka/getVerifiCode',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "get",
          data: {
            mobile: this.data.mobile
          },
          success: function (res) {
            that.getTime();
            if (res.data.success) {} else {}
          }
        })
      }
    }
  },
  //获取验证码时候显示时间
  getTime() {
    var that = this;
    var timer = setInterval(function () {
      that.data.timeNum--;
      if (that.data.timeNum <= 0) {
        that.setData({
          tipsCode: "重新获取验证码",
          timeNum: timeOut,
          clikType: false
        });
        clearInterval(timer);
      } else {
        that.setData({
          tipsCode: "发送" + that.data.timeNum + "秒",
        })
      }
    }, 1000)
  },


  getPhoneNumber: function (e) {

    app.getOpenid();

    var that = this;
    var detail = e.detail;

    wx.checkSession({
      success: function (res) {

      },
      fail: function (res) {

        // app.getOpenid();
      },
      complete() {




        var openId = wx.getStorageSync("OPENID");

        var SESSION_KEY = wx.getStorageSync('SESSION_KEY');


        if (e.detail.errMsg != "getPhoneNumber:ok") {
          wx.showToast({
            title: '拒绝授权，请用其他方式登录',
            icon: 'success',
          })
          return
        }

        if (!openId) {
          app.getOpenid();
        }
        if (openId) {

          wx.request({
            url: applet_park_api_url + '/api/wechatApplet/getUserInfo',
            method: 'GET',
            data: {
              "sessionKey": wx.getStorageSync('SESSION_KEY'),
              "encryptedData": detail.encryptedData,
              "iv": detail.iv
            },
            success: function (res) {


              var userInfo = wx.getStorageSync("USER_INFO");

              if (res.data.code == 0) {
                userInfo.bindPhone = res.data.data.mobile;
                userInfo.nickName = res.data.data.nickName;
                wx.setStorageSync("USER_INFO", userInfo);
                wx.setStorageSync("nickName", res.data.data.nickName);
                wx.setStorageSync("id", res.data.data.id);
                wx.setStorageSync("userRoles", res.data.data.userRoles);

                wx.showToast({
                  title: '登录成功',
                  icon: 'success',
                })
                wx.reLaunch({
                  url: '../loginidentityList/loginidentityList',

                })

              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                });
              }
            },
            fail: function () {
              wx.showModal({
                title: '获取用户手机号失败',
                showCancel: false,
                confirmText: '确认',
                success(res) {},
              })
            }
          })
        }
      }
    })
  },
  // 立即注册
  register: function () {

    var that = this;
    var userInfo = wx.getStorageSync("USER_INFO");
    var openId = wx.getStorageSync("OPENID");
    wx.request({
      url: applet_park_api_url + "/wxuser/registerNoAuth",
      data: {
        openid: openId,
        phone: wx.getStorageSync('phoneNumber'),
        nickname: userInfo.nickName,
        appId: app.globalData.appid
      },
      method: 'post',
      success: function (res) {
        if (res.data.success) {
          userInfo.bindPhone = wx.getStorageSync('phoneNumber');
          wx.setStorageSync("USER_INFO", userInfo);
          wx.switchTab({
            url: '../user_center/user_center',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: function (res) {

      }
    })

  },

  accountPassword: function () {
    var userInfo = wx.getStorageSync("USER_INFO");

    if (this.data.userName == '' || this.data.userName == null) {
      wx.showToast({
        title: '账号不能为空',
        icon: 'none',
      })
      return
    }
    if (this.data.password == '' || this.data.password == null) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
      })
      return
    }
    var that = this;
    wx.request({
      url: applet_park_api_url + '/login',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        "userName": that.data.userName,
        "password": that.data.password,
        "rememberMe": true

      },
      success: function (res) {
        if (res.data.code == 0) {
          userInfo.bindPhone = res.data.data.mobile;
          wx.setStorageSync("USER_INFO", userInfo);
          wx.setStorageSync("id", res.data.data.id);
          wx.setStorageSync("nickName", res.data.data.nickName);
          // wx.setStorageSync("userRoles", res.data.data.userRoles);
          wx.setStorageSync("userRoles", res.data.data.userRoles);
          var i = res.data.data.userRoles.findIndex((e) => {
            return e == 'checkLeaderByLocation' || e == 'checkLeaderByDevice';
          });

          // if (i > -1) {
          //   wx.setStorageSync("isLeader", true);

          // }


          wx.showToast({
            title: '登录成功',
            icon: 'success',
          })
          wx.reLaunch({
            url: '../loginidentityList/loginidentityList',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {}

    })
  },
  phoneLogin() {
    var openId = wx.getStorageSync("OPENID");
    var that = this;
    if (this.data.mobile == '' || this.data.mobile == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
      return
    }

    wx.request({
      url: applet_park_api_url + '/login',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        "userName": that.data.mobile,
        "openId": openId,
        "rememberMe": true

      },
      success: function (res) {
        if (res.data.code == 0) {
          userInfo.bindPhone = res.data.data.mobile;
          wx.setStorageSync("USER_INFO", userInfo);
          wx.setStorageSync("id", res.data.data.id);
          wx.setStorageSync("nickName", res.data.data.nickName);
          // wx.setStorageSync("userRoles", res.data.data.userRoles);
          wx.setStorageSync("userRoles", res.data.data.userRoles);
          var i = res.data.data.userRoles.findIndex((e) => {
            return e == 'checkLeaderByLocation' || e == 'checkLeaderByDevice';
          });

          wx.showToast({
            title: '登录成功',
            icon: 'success',
          })

          // wx.switchTab({
          //   url: '../my/my',
          // })
          wx.reLaunch({
            url: '../loginidentityList/loginidentityList',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {

      }

    })
  },
})